package org.contextsensitivetodo.context.rules.time;

import org.contextsensitivetodo.context.rules.SuperContextRule;

import android.text.format.Time;
import android.util.Log;

public class TimeOfTheDayContextRule extends SuperContextRule {

	private TimeRuleTime afterThisTime;
	private TimeRuleTime beforeThisTime;

	

	public TimeOfTheDayContextRule(TimeRuleTime afterThisTime,
			TimeRuleTime beforeThisTimbe) {
		super();
		this.afterThisTime = afterThisTime;
		this.beforeThisTime = beforeThisTimbe;
	}



	public void doStateUpdate() {

		Log.d("cstodo", "Updating time of the day context rule");
		
		Time now = new Time();
		now.setToNow();
		int hoursNow = now.hour;
		int minutesNow = now.minute;
		
		
		

		String nowStringPresentation = ""
				+ (hoursNow < 10 ? "0" + hoursNow : hoursNow)
				+ (minutesNow < 10 ? "0" + minutesNow : minutesNow);

		
		Log.d("cstodo", "Evaluating time of day. Current time:" + nowStringPresentation + " rule after this time: "+ afterThisTime.getStringPresentation() + " rule before thie time: " + this.beforeThisTime.getStringPresentation());
		
		
		if (this.afterThisTime.getStringPresentation().compareTo(
				nowStringPresentation) <= 0
				&& this.beforeThisTime.getStringPresentation().compareTo(
						nowStringPresentation) > 0) {
			this.setNewState(true);
		} else {
			this.setNewState(false);
		}
		
	}
	
	public String getRuleName() {
		return "TimeOfTheDayContextRule, after: " + afterThisTime.getStringPresentation()+" before: " + beforeThisTime.getStringPresentation();
	}

}
